/**
 * Created by Chris on 14-4-15.
 */
define('aitang_upload', [], function(){
    function upload_file(options){
        var progressCallback  = options.progress,
            successCallback  = options.success,
            errorCallback  = options.error,
            url = options.url,
            field = options.field,
            file = options.file;
        var fd = new FormData();
//        var file = document.getElementById('file').files[0];
        fd.append(field, file);
        var xhr = new XMLHttpRequest();
        if (progressCallback){
            xhr.upload.addEventListener('progress', progressCallback, false);
        }
        xhr.open('POST', url);
        xhr.onreadystatechange = function(){
            console.log("state change!state="+xhr.readyState+" xhr.status["+xhr.status+"]");
            if (xhr.readyState==4){
                if (xhr.status==200){
                    successCallback(xhr.responseText);
                }else{
                    errorCallback(xhr);
                }
            }
        };
        xhr.send(fd);
    }

    return {
        upload_file : upload_file
    }

});